package com.pomelo.tiku.frgs

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.pomelo.tiku.R
import com.pomelo.tiku.activities.OritationVocationActivity
import com.pomelo.tiku.adapters.SubjectAdapter
import com.pomelo.tiku.constants.SharePreferenceConstants
import com.pomelo.tiku.managers.SharedPreferenceManager
import com.pomelo.tiku.utils.ClickUtils
import com.pomelo.tiku.views.CommonToast
import kotlinx.android.synthetic.main.activity_oritation_vocation.*
import kotlinx.android.synthetic.main.fragment_question.*
import kotlinx.android.synthetic.main.fragment_question.view.*


class FragmentQuestion : BaseFragment(),View.OnClickListener {
    private var mView: View? = null
    private var isSelectJob = false
    private val REQUEST_CODE_1 = 1
    private val REQUEST_CODE_2 = 2

    private var listSubject: ArrayList<String>? = null
    private var subjectAdapter: SubjectAdapter? = null

    private var isLogin = false

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        initUserInfo()
        mView = inflater.inflate(R.layout.fragment_question, container, false)
        initView(mView!!)
        return mView
    }

    private fun initView(view: View) {
        if(isSelectJob) {
            view.cl_rootview_job_unselected.visibility = View.GONE
            view.ll_subject.visibility = View.VISIBLE
        } else {
            view.ll_subject.visibility = View.GONE
            view.cl_rootview_job_unselected.visibility = View.VISIBLE
            view.txt_orientate_vocation_edit.setOnClickListener(this)
//            view.txt_recommend_subject_edit.setOnClickListener(this)
        }
    }

    private fun initUserInfo() {
        isLogin = SharedPreferenceManager.getInstance(this.context).get(SharePreferenceConstants.KEY_ISLOGIN,false) as Boolean
    }

    override fun onClick(view: View?) {
        when(view!!.id) {
            R.id.txt_orientate_vocation_edit -> {
                if(ClickUtils.isDoubleClick(txt_orientate_vocation_edit)) {
                    return
                }
                if(!isLogin) {
                    context?.let { CommonToast.show(it,"您还没登录，请先登录") }
                    return
                }
                startActivityForResult(Intent(this.activity, OritationVocationActivity::class.java),REQUEST_CODE_1)

            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == REQUEST_CODE_1) {
            if(resultCode == 1000 && data != null) {
                if(TextUtils.isEmpty(data.getStringExtra("data_occupation")) ) {
                    return
                }

                if(mView != null) {
                    mView!!.txt_orientate_vocation_edit.text = data.getStringExtra("data_occupation")
                    mView!!.ll_subject.visibility = View.VISIBLE
                    mView!!.txt_recommend_subject_edit.visibility = View.GONE
                    mView!!.view_two.visibility =View.GONE
                    isSelectJob = true
                    initSubjectRv(mView!!)
                }

            }
        }
    }

    private fun initSubjectRv(view: View) {
        listSubject = ArrayList()
        listSubject!!.add("哈哈哈哈")
        listSubject!!.add("哈哈哈哈")
        listSubject!!.add("哈哈哈哈")
        listSubject!!.add("哈哈哈哈")
        listSubject!!.add("哈哈哈哈")
        subjectAdapter = SubjectAdapter(listSubject)
        val layoutManager = GridLayoutManager(this.context,3)
        view.rv_subject.layoutManager = layoutManager
        view.rv_subject.adapter = subjectAdapter
    }

}